package com.young.springdemo.service.iterator;

import java.util.List;

//具体迭代器类
public class ProductIterator implements Iterator {
    private List<Object> products;
    private int cursorAsc;
    private int cursorDesc;

    public ProductIterator(ProductList list) {
        this.products = list.getObjects();
        cursorAsc = 0;
        cursorDesc = products.size() - 1;
    }


    @Override
    public void next() {
        cursorAsc++;

    }

    @Override
    public boolean isLast() {
        return (cursorAsc == products.size());
    }

    @Override
    public void previous() {
        if (cursorDesc > -1) {
            cursorDesc--;
        }
    }

    @Override
    public boolean isFirst() {
        return (cursorDesc == -1);
    }

    @Override
    public Object getNextObject() {
        return products.get(cursorAsc);
    }

    @Override
    public Object getPreviousObject() {
        return products.get(cursorDesc);
    }

    @Override
    public void getPrevious() {
        cursorDesc--;
    }
}
